layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;


    $.ajax({
        url:"/classes/selectall?page=1&limit=100",
        type:"get",
        success: function (data) {
            data=data.data;
            console.info(data)
            //使用循环遍历，给下拉列表赋值
            $.each(data, function (index, value) {
                $('#classid').append(new Option(value.cname,value.id));// 下拉菜单里添加元素
            });
            layui.form.render("select");//重新渲染 固定写法
        }
    })

    //学生列表实例
    var tableIns = table.render({
        elem: '#newsList',
        url : '/student/selectall',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 10,
        limits : [5,10,15,20],
        id : "newsListTable",/*此id缺失则从原始html中获取id*/
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'id', title: 'ID', width:60, align:"center",sort:true},
            {field: 'name', title: '姓名', width:90},
            {field: 'snumber', title: '学号', align:'center'},
            {field: 'sex', title: '性别',  align:'center',templet:function(d){
                return d.sex==0?"女":"男";
                }},
            {field: 'age', title: '年龄', width:90},
            {field: 'cname', title: '班级',  align:'center',templet:function(d){
                    return d.classes.cname;
                }},

            {field: 'createtime', title: '创建时间', align:'center', minWidth:110, templet:function(d){
                    var time=new Date(d.createtime);
                    return time.toLocaleDateString()+ " "+time.toTimeString().slice(0,8)
            }},
            {title: '操作', width:170, templet:'#newsListBar',fixed:"right",align:"center"}
        ]]
    });



    //搜索
    $(".search_btn").on("click",function(){
            table.reload("newsListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                   name: $(".searchVal").val(),
                   snumber: $(".searchNumVal").val(),
                    class_id: $(".searchClassVal").val()
                }
            })
    });

    //添加文章
    function addNews(edit){
        var index = layui.layer.open({
            title : "添加学生",
            type : 2,
            content : "/router/tostudentadd",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){

                    body.find(".name").val(edit.name);
                    body.find(".snumber").val(edit.snumber);
                    body.find(".age").val(edit.age);
                    body.find(".id").val(edit.id);
                    body.find("select[name=class_id]").val(edit.class_id);

                    body.find("input[name=sex]").each(function(){
                        if($(this).val()==edit.sex){
                            $(this).prop("checked",true)
                        }
                    })

                    var time=new Date(edit.createtime);
                        time=time.toLocaleDateString()+ " "+time.toTimeString().slice(0,8);
                    body.find(".createtime").val(time);
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".addNews_btn").click(function(){
        addNews();
    })

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('newsListTable'),
            data = checkStatus.data,
            ids = [];
            if(data.length > 0) {
            for (var i in data) {
                ids.push(data[i].id);
            }

            layer.confirm('确定删除选中的学生？', {icon: 3, title: '提示信息'}, function (index) {
                $.ajax({
                    type:"post",
                    url:"/student/deletedisp",
                    data:JSON.stringify(ids),
                    contentType:"application/json",
                    success: function (data) {
                        layer.msg(data.msg);
                        tableIns.reload({
                            done:function(res, curr, count){
                                if(res.data.length==0){
                                    curr=curr-1;
                                    tableIns.reload({
                                        page:{
                                            curr:curr
                                        }
                                    })
                                }

                            }
                        })
                        layer.close(index);
                    }
                })
            })
        }else{
            layer.msg("请选择需要删除的学生");
        }
    })

    //列表操作
    table.on('tool(newsList)', function(obj){
        var layEvent = obj.event,/*事件类型*/
            data = obj.data;/*拿到整行数据*/


        if(layEvent === 'edit'){ //编辑
            addNews(data);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此学生？',{icon:3, title:'提示信息'},function(index){
                $.get("/student/delete",{
                    id : data.id  //将需要删除的Id作为参数传入
                },function(data){
                    layer.msg(data.msg);
                    tableIns.reload({
                        done:function(res, curr, count){
                            if(res.data.length==0){
                                curr=curr-1;
                                tableIns.reload({
                                    page:{
                                        curr:curr
                                    }
                                    })
                            }

                        }
                    })
                    layer.close(index);
                })
            });
        }
    });

})